import re
from time import sleep

import requests
from bs4 import BeautifulSoup


class First_data:
    list_data = [0, 0, 0, 0]
    list_cat = [0, 0]
    list = []
    all_list = []
    secone_list=[]

    def __init__(self, col):
        self.col = col
        # 正则表达式爬取到子链接
        self.link = re.compile(r'<div class="info"><a class="title" href="//(.*?)" target="_blank">')

        # 模拟浏览器请求头
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                          ' Chrome/102.0.0.0 Safari/537.36'
        }
        # 请求地址url
        self.url = f'https://www.bilibili.com/v/popular/rank/{col}'
        self.res = requests.get(self.url, headers=self.headers)
        # 使用bs4解析网页
        self.soup = BeautifulSoup(self.res.text, 'lxml')
        self.video_info = self.soup.select(".info")
        # print(self.video_info)

    def get_list_first_ALLdata(self):
        for video in self.video_info:
            # 提取标签
            for i in range(0, len(video.select(".rank-tab li"))):
                if len(video.select('.rank-tab li').text) >= 1:
                    types = video.select('.rank-tab li')
                    # print(types)

            # if len(video.select('.detail-state span')) >= 1:
            #     broadcast = video.select('.detail-state span')[0].text.strip()
            #     if "万" not in broadcast:
            #         continue
            # 去掉为空的a标签干扰项
            if len(video.select('.info a')) >= 1:
                # 提取到标题
                title = video.select('.info a')[0].text.strip()

            if len(video.select('.detail span')) >= 1:
                # 提取up主名称
                up_name = video.select('.detail span')[0].text.strip()

            if len(video.select('.detail-state span')) >= 1:
                # 提取出视频热度
                broadcast = video.select('.detail-state span')[0].text.strip()
                if "万" in broadcast:
                    broadcast = str(float(broadcast.replace('万', '')) * 10000)

            if len(video.select('.detail-state span')) >= 1:
                # 提取出评论人数
                barrage = video.select('.detail-state span')[1].text.strip()
                if "万" in barrage:
                    barrage = str(float(barrage.replace('万', '')) * 10000)
                # print(barrage)
            self.all_list.append(f"{title},{up_name},{broadcast},{barrage}")
        with open('data/save1.csv', 'a', encoding='utf-8') as fp:
            # 标题，up名称，播放量，评论数
            for all_list_data in self.all_list:
                fp.write(all_list_data+"\n")
                fp.flush()
        print(f"\n-{self.col}-爬取完成！！！")

    def get_sencond_ALLdata(self, new_url):

        second_url = 'https://' + new_url
        second_res = requests.get(second_url, headers=self.headers)
        # print(res.text)
        second_soup = BeautifulSoup(second_res.text, 'lxml')
        second_video_info = second_soup.select(".video-container-v1")
        for video in second_video_info:
            # 获取标题
            if len(video.select('.video-info-v1')) >= 1:
                title = video.select('.video-info-v1 h1')[0].text
            if len(video.select('.toolbar-left span')) >= 1:
                # 获取点赞
                good = video.select('.toolbar-left span')[0].text
                if "万" in good:
                    good = str(float(good.replace('万', '')) * 10000)
                if "亿" in good:
                    good = str(float(good.replace('万', '')) * 10000000)
                # print(good)
            if len(video.select('.toolbar-left span')) >= 1:
                # 获取投币
                coin = video.select('.toolbar-left span')[2].text
                if "万" in coin:
                    coin = str(float(coin.replace('万', '')) * 10000)
                if "亿" in coin:
                    good = str(float(coin.replace('万', '')) * 10000000)
                # print(coin)
            if len(video.select('.toolbar-left span')) >= 1:
                collect = video.select('.toolbar-left span')[4].text
                if "万" in collect:
                    collect = str(float(collect.replace('万', '')) * 10000)
                # print(collect)
            if len(video.select('.toolbar-left span')) >= 1:
                transmit = video.select('.toolbar-left span')[6].text
                if "万" in transmit:
                    transmit = str(float(transmit.replace('万', '')) * 10000)
                # print(transmit)
                label = video.select(".tag-wrap li")
                # print(label)

                for i in range(0, len(video.select(".tag-wrap li"))):
                    self.list.append(label[i].text.strip())
                # print(list)
            self.secone_list.append(f"{title},{good},{coin},{collect},{transmit}")
        with open('data/save2.csv', 'a', encoding='utf-8') as fp:
            for str1 in self.secone_list:
                fp.write(str1+"\n")
                fp.flush()
        print(f"写入成功---内置网页")

    def get_link(self):
        linking = re.findall(self.link, str(self.video_info))
        for i in range(len(linking)):
            if len(self.video_info[i].select('.info a')) >= 1:
                # 提取到标题
                title = self.video_info[i].select('.info a')[0].text.strip()
                if '「原神生日会」' in title:
                    self.get_yuanshen_link(linking[i])
                if '【原神生日会】' in title:
                    self.get_yuanshen_link(linking[i])
                else:
                    # self.content_second(linking[i])
                    self.get_sencond_ALLdata(linking[i])

    def get_yuanshen_link(self, new_url):
        list_infos = [0, 0, 0, 0]
        second_url = 'https://' + new_url
        res = requests.get(second_url, headers=self.headers)
        soup = BeautifulSoup(res.text, 'lxml')
        second_video_info = soup.select(".video-toolbar-content_left")
        second_video_info = str(second_video_info)
        second_video_info = BeautifulSoup(second_video_info, 'lxml')
        data = second_video_info.text.split('\n')
        list_infos = [data[1].strip(), data[3].strip(), data[5].strip(), data[6].split(']')[0].strip()]

        for i, list_info in enumerate(list_infos, 0):
            data = float(list_info.replace('万', '')) * 10000
            self.list_data[i] += data

    def content_second(self, new_url):

        second_url = 'https://' + new_url
        second_res = requests.get(second_url, headers=self.headers)

        second_soup = BeautifulSoup(second_res.text, 'lxml')
        second_video_info = second_soup.select(".video-container-v1")
        for video in second_video_info:
            # 获取标题
            if len(video.select('.video-info-v1')) >= 1:
                title = video.select('.video-info-v1 h1')[0].text
            if len(video.select('.toolbar-left span')) >= 1:
                # 获取点赞
                good = video.select('.toolbar-left span')[0].text
                if "万" in good:
                    good = str(float(good.replace('万', '')) * 10000)
                else:
                    good = str(float(good))

            if len(video.select('.toolbar-left span')) >= 1:
                # 获取投币
                coin = video.select('.toolbar-left span')[2].text
                if "万" in coin:
                    coin = str(float(coin.replace('万', '')) * 10000)
                else:
                    coin = str(float(coin))

            if len(video.select('.toolbar-left span')) >= 1:
                # 获取收藏数
                collect = video.select('.toolbar-left span')[4].text
                if "万" in collect:
                    collect = str(float(collect.replace('万', '')) * 10000)
                else:
                    collect = str(float(collect))

            if len(video.select('.toolbar-left span')) >= 1:
                # 获取转发数
                transmit = video.select('.toolbar-left span')[6].text
                if "万" in transmit:
                    transmit = str(float(transmit.replace('万', '')) * 10000)
                if '分享' in transmit:
                    transmit = 0
                else:
                    transmit = str(float(transmit))

            self.list_data[0] += float(good)
            self.list_data[1] += float(coin)
            self.list_data[2] += float(collect)
            self.list_data[3] += float(transmit)

            sleep(1)


def get_first_data():
    # "国创相关,动画,音乐,舞蹈,游戏,知识,科技,运动,汽车,生活,美食,动物圈,鬼畜,时尚,娱乐,影视,原创,新人"
    find_data = ['guochuang', 'douga', 'music', 'dance', 'game', 'knowledge',
                 'tech', 'sports', 'car', 'life', 'food', 'animal', 'kichiku',
                 'fashion', 'ent', 'cinephile', 'origin', 'rookie']
    for data_ in find_data:
        start = First_data(data_)
        start.get_list_first_ALLdata()
        start.get_link()
        # start.save_link_data()
        sleep(2)


if __name__ == '__main__':
    # get_first_data()
    pass